package com.amadeus.commons.log.dao;

import com.amadeus.commons.log.entity.ExceptionInstance;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;

public class MongoExceptionInstanceDAO implements ExceptionInstanceDAO {

	private static MongoExceptionInstanceDAO _instance;
	private final DBCollection coll;

	private MongoExceptionInstanceDAO(Mongo m) {
		DB db = m.getDB("logexplorer");
		coll = db.getCollection("exceptions");
	}

	public static synchronized ExceptionInstanceDAO getInstance(Mongo m) {
		if (_instance == null) {
			_instance = new MongoExceptionInstanceDAO(m);
		}
		return _instance;
	}

	public void insert(ExceptionInstance exceptionInstance) {

		BasicDBObject doc = new BasicDBObject();

		doc.put("exceptiontype", exceptionInstance.getExceptionType().getType());
		doc.put("jsessionid", exceptionInstance.getJsi());
		doc.put("timestamp", exceptionInstance.getDate());
		doc.put("stacktrace", exceptionInstance.getExceptionStackTrace().getStackTrace());
		doc.put("hashcode", exceptionInstance.getExceptionStackTrace().getHashCode());

		coll.insert(doc);

	}
}
